//
//  GainTopCell.swift
//  DSP
//
//  Created by tucici on 2024/5/17.
//

import UIKit
protocol GainTopCellDelegate{
    func select(ch:ChannelModel, index:Int)
    func swipe(ch:ChannelModel, index:Int)

}
class GainTopCell: UITableViewCell {
    private var collectionView : UICollectionView!
    var delegate:GainTopCellDelegate?
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        initUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func initUI(){
        backgroundColor = .clear
        selectionStyle = .none
        
        let flowlayout = UICollectionViewFlowLayout()
        flowlayout.scrollDirection = .horizontal
        flowlayout.minimumLineSpacing = 10.0.k_w
        flowlayout.minimumInteritemSpacing = 10.0.k_h
        flowlayout.sectionInset = UIEdgeInsets(top: 10.k_h, left: 10.k_w, bottom: 10.k_h, right: 10.k_w)
        collectionView = UICollectionView(frame:.zero,collectionViewLayout:flowlayout)
        collectionView.backgroundColor = .clear
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.showsVerticalScrollIndicator = false
        collectionView.register(GainTopItem.nib(), forCellWithReuseIdentifier:GainTopItem.ID())
        
        contentView.addSubview(collectionView)
        
        collectionView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
            make.height.equalTo(0)
        }
    }
    
    var array = [ChannelModel](){
        didSet{
            guard let flowlayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout else{return}
            let w = itemWidth(flowlayout, array.count)
            flowlayout.itemSize = CGSize(width: w, height: 290.k_h)
            let h = collectionH(flowlayout, array, array.count)
            collectionView.reloadData()
            collectionView.snp.updateConstraints { make in
                make.height.equalTo(h)
            }
        }
    }
}

//MARK: - UICollectionViewDataSource
extension GainTopCell:UICollectionViewDataSource{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return array.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier:GainTopItem.ID(), for:indexPath) as! GainTopItem
        let model = array[indexPath.row]
        cell.model = model
        cell.block = {[weak self]  in
            self?.delegate?.select(ch: model, index: indexPath.row)
        }
        cell.swipeBlock = {[weak self] in
            self?.delegate?.swipe(ch: model, index: indexPath.row)
        }
        return cell
    }
}
//MARK: - UICollectionViewDelegate
extension GainTopCell:UICollectionViewDelegate{
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath){
//        let ch = array[indexPath.row]
//        if ch.select || ch.enable == false {return}
//        ch.select = true
//        delegate?.select(ch: ch)
    }
}

